package com.iflytek.uoamp.agent.monitor.internet;

import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentConfig;

import java.util.Map;

import org.apache.log4j.Logger;

/**
 * Created by Administrator on 2017/2/21.
 */
public class InterNetSpeedTest implements Runnable{
    private static volatile InterNetSpeedTest instance;

    private String ip;

    private InterNetSpeedTest(String ip){
        this.ip = ip;
    }

    public static InterNetSpeedTest getInstance(String ip) {
        if (instance == null) {
            synchronized (InterNetSpeedTest.class) {
                if (instance == null) {
                    instance = new InterNetSpeedTest(ip);
                }
            }
        }
        return instance;
    }

    public static void setInstance(InterNetSpeedTest instance) {
        InterNetSpeedTest.instance = instance;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    @Override
    public void run() {
    	Logger.getRootLogger().info("InterNetSpeedTest Start!");
        while (true){
            String result = "";
            String downloadSpeed = "0.0";
            String uploadSpeed = "0.0";
            Logger.getRootLogger().info("**********************new*************************");
//            System.out.println("**********************new*************************");
            Map<String , Object> uploadContent = InterNetSpeed.getInstance().getUploadContent();
            if(!uploadContent.isEmpty()){
                 for(String key :uploadContent.keySet()){
                     if(key.equals(ip)){
                         uploadSpeed = uploadContent.get(key).toString();
//                         System.out.println("uploadContent:" + key + " " + uploadSpeed + "kb/s");
//                         Logger.getRootLogger().info("uploadContent:" + key + " " + uploadSpeed + "kb/s");
                     }
                 }
                uploadContent.clear();
            }
            Map<String , Object> downloadContent = InterNetSpeed.getInstance().getDownloadContent();
            if(!downloadContent.isEmpty()){
                for(String key :downloadContent.keySet()){
                    if(key.equals(ip)){
                        downloadSpeed = downloadContent.get(key).toString();
//                        System.out.println("downloadContent:" + key + " " + downloadSpeed + "kb/s");
//                        Logger.getRootLogger().info("downloadContent:" + key + " " + downloadSpeed + "kb/s");
                    }
                }
                downloadContent.clear();
            }
            result = MCAgentConfig.getInstance().getMonitoredPointIp() + "------------->" + ip + ": " + uploadSpeed + "kb/s     " + MCAgentConfig.getInstance().getMonitoredPointIp() + "<-------------" + ip + ": " + downloadSpeed + "kb/s";
//            System.out.println(result);
            Logger.getRootLogger().info(result);
//            System.out.println("**********************end***************************\n");
            Logger.getRootLogger().info("**********************end*************************");

            try {
                Thread.currentThread().sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
